/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-3 下午3:57
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.adapter.consulation;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.bean.consulation.ConsulationData;
import com.example.dqhl.jdmetro.config.Config;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dqhl on 17/6/27.
 */
public class ConsulationDataAdapter extends RecyclerView.Adapter<ConsulationDataAdapter.ConsulationDataViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<ConsulationData.DataBean> mConsulationDataList = new ArrayList<>();
    private List<ConsulationData.DataBean.PicAllBean> picAllBeen = new ArrayList<>();
    private onItemClickListener onItemClickListener;

    public ConsulationDataAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }

    public void refresh_list(List<ConsulationData.DataBean> consulationDatas) {
        this.mConsulationDataList = consulationDatas;
        notifyDataSetChanged();
    }

    //点击事件和长按事件
    public interface onItemClickListener {
        void onItemClick(View view, int position);

    }
    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    @Override
    public ConsulationDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ConsulationDataViewHolder consulationDataViewHolder = new ConsulationDataViewHolder(mInflater.inflate(R.layout.layout_consulation_data, parent, false));
        return consulationDataViewHolder;
    }

    @Override
    public void onBindViewHolder(final ConsulationDataViewHolder holder, int position) {
        picAllBeen = mConsulationDataList.get(position).getPic_all();
        //根据图片数量去判断显示那个布局
        // 0  纯文本 1 一张图片 2  两张图片 3  多张图片
        if (mConsulationDataList.get(position).getPic_number() == 0) {
            holder.ll_text.setVisibility(View.VISIBLE);
            holder.rl_onePic.setVisibility(View.GONE);
            holder.ll_twoPic.setVisibility(View.GONE);
            holder.ll_morePic.setVisibility(View.GONE);
            holder.tv_text.setText(mConsulationDataList.get(position).getTitle_a());
        } else if (mConsulationDataList.get(position).getPic_number() == 1) {
            holder.ll_text.setVisibility(View.GONE);
            holder.rl_onePic.setVisibility(View.VISIBLE);
            holder.ll_twoPic.setVisibility(View.GONE);
            holder.ll_morePic.setVisibility(View.GONE);
            holder.tv_oneText.setText(mConsulationDataList.get(position).getTitle_a());
//            for (int i = 0; i < picAllBeen.size(); i++) {
            Picasso.with(mContext).load(Config.pic_base_url + picAllBeen.get(0).getPic()).into(holder.iv_onePic);
//                x.image().bind(holder.iv_onePic, Config.pic_base_url + picAllBeen.get(0).getPic());
//            }
        } else if (mConsulationDataList.get(position).getPic_number() == 2) {
            holder.ll_text.setVisibility(View.GONE);
            holder.rl_onePic.setVisibility(View.GONE);
            holder.ll_twoPic.setVisibility(View.VISIBLE);
            holder.ll_morePic.setVisibility(View.GONE);
            holder.tv_twoText.setText(mConsulationDataList.get(position).getTitle_a());
            Picasso.with(mContext).load(Config.pic_base_url + picAllBeen.get(0).getPic()).into(holder.iv_twoPicOne);
            Picasso.with(mContext).load(Config.pic_base_url + picAllBeen.get(1).getPic()).into(holder.iv_twoPicTwo);
//            x.image().bind(holder.iv_twoPicOne, Config.pic_base_url+picAllBeen.get(0).getPic());
//            x.image().bind(holder.iv_twoPicTwo, Config.pic_base_url+picAllBeen.get(1).getPic());
        } else {
            holder.ll_text.setVisibility(View.GONE);
            holder.rl_onePic.setVisibility(View.GONE);
            holder.ll_twoPic.setVisibility(View.GONE);
            holder.ll_morePic.setVisibility(View.VISIBLE);
            holder.tv_moreText.setText(mConsulationDataList.get(position).getTitle_a());
            Picasso.with(mContext).load(Config.pic_base_url + picAllBeen.get(0).getPic()).into(holder.iv_morePicOne);

            Picasso.with(mContext).load(Config.pic_base_url + picAllBeen.get(1).getPic()).into(holder.iv_morePicTwo);

            Picasso.with(mContext).load(Config.pic_base_url + picAllBeen.get(2).getPic()).into(holder.iv_morePicThree);

//            x.image().bind(holder.iv_morePicOne, Config.pic_base_url+picAllBeen.get(0).getPic());
//            x.image().bind(holder.iv_morePicTwo, Config.pic_base_url+picAllBeen.get(1).getPic());
//            x.image().bind(holder.iv_morePicThree, Config.pic_base_url+picAllBeen.get(2).getPic());
        }
        if(onItemClickListener != null){
            holder.ll_consulationitem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mConsulationDataList.size();
    }

    class ConsulationDataViewHolder extends RecyclerView.ViewHolder {
        //总布局
        LinearLayout ll_consulationitem;
        //只有文本
        LinearLayout ll_text;
        TextView tv_text;
        //一张图片
        RelativeLayout rl_onePic;
        ImageView iv_onePic;
        TextView tv_oneText;
        //两张图片
        LinearLayout ll_twoPic;
        TextView tv_twoText;
        ImageView iv_twoPicOne;
        ImageView iv_twoPicTwo;

        //三张/多张图片
        LinearLayout ll_morePic;
        TextView tv_moreText;
        ImageView iv_morePicOne;
        ImageView iv_morePicTwo;
        ImageView iv_morePicThree;

        public ConsulationDataViewHolder(View view) {
            super(view);
            ll_consulationitem = (LinearLayout) view.findViewById(R.id.ll_consulationitem);
            ll_text = (LinearLayout) view.findViewById(R.id.ll_text);
            (tv_text) = (TextView) view.findViewById(R.id.tv_text);

            rl_onePic = (RelativeLayout) view.findViewById(R.id.rl_onePic);
            (tv_oneText) = (TextView) view.findViewById(R.id.tv_oneText);
            (iv_onePic) = (ImageView) view.findViewById(R.id.iv_onePic);

            ll_twoPic = (LinearLayout) view.findViewById(R.id.ll_twoPic);
            (tv_twoText) = (TextView) view.findViewById(R.id.tv_twoText);
            (iv_twoPicOne) = (ImageView) view.findViewById(R.id.iv_twoPicOne);
            (iv_twoPicTwo) = (ImageView) view.findViewById(R.id.iv_twoPicTwo);

            ll_morePic = (LinearLayout) view.findViewById(R.id.ll_morePic);
            (tv_moreText) = (TextView) view.findViewById(R.id.tv_moreText);
            (iv_morePicOne) = (ImageView) view.findViewById(R.id.iv_morePicOne);
            (iv_morePicTwo) = (ImageView) view.findViewById(R.id.iv_morePicTwo);
            (iv_morePicThree) = (ImageView) view.findViewById(R.id.iv_morePicThree);
        }
    }
}
